local hetao = fk.CreateSkill{
  name = "swis__hetao",
}

local U = require "packages/utility/utility"
local SeabornUtil = require "packages/seaborn/utility/utility"

Fk:loadTranslationTable{
  ["swis__hetao"] = "合讨",
  [":swis__hetao"] = "出牌阶段限一次，你可以指定一名其他角色，令除其以外的角色议事。"
  .."每名角色将其意见牌按以下规则对指定的角色使用，不能使用则弃置：红色牌当【杀】；黑色牌当【桃】。",
  ["#swis__hetao-prompt"] = "合讨：指定一名其他角色，令除其以外的角色议事",

  ["$swis__hetao"] = "合诸侯之群力，扶大汉之将倾。",
}

hetao:addEffect("active", {
  anim_type = "offensive",
  prompt = "#swis__hetao-prompt",
  card_num = 0,
  target_num = 1,
  can_use = function(self, player)
    return player:usedSkillTimes(hetao.name, Player.HistoryPhase) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = function (self, player, to_select, selected, selected_cards)
    return #selected == 0 and player ~= to_select
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]

    local targets = table.filter(room:getOtherPlayers(target), function(p)
      return not p:isKongcheng()
    end)
    room:delay(1000)
    room:doIndicate(player, targets)
    local discussion = U.Discussion(player, targets, hetao.name)
    if not discussion then return end

    for _, p in ipairs(room:getAlivePlayers(true)) do
      local result = SeabornUtil.getDiscussionOpinion(p, discussion)
      if result then
        local card
        if result[1] == "red" then
          card = Fk:cloneCard("slash")
        elseif result[1] == "black" then
          card = Fk:cloneCard("peach")
        end
        if card then
          card.skillName = hetao.name
          card:addSubcard(result[2])
        end

        local owner = room:getCardOwner(card)
        if owner and owner == p then
          local can_use
          if card.trueName == "slash" then
            can_use = p:canUseTo(card, target, {bypass_distances = true, bypass_times = true})
          else
            can_use = target:isWounded() and not p:isProhibited(target, card)
          end

          if can_use then
            room:useCard{
              from = p,
              tos = {target},
              card = card,
              extraUse = true,
            }
          else
            room:throwCard(card, hetao.name, p, p)
          end
        end
      end
    end
  end,
})

return hetao